package com.leador.socket;

import android.os.Handler;
import android.util.Log;

import com.leador.module.ILocation;
import com.leador.utils.Config;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.Socket;
import java.util.List;

/**
 * Created by liuha on 2016/9/14.
 */
public class WriteThread extends Thread{
    private SocketMananger socketMananger ;
    private byte[] bytes = null;
    private static ByteArrayOutputStream baos = null;
    public WriteThread(SocketMananger socketMananger,byte[] bytes){
        this.socketMananger = socketMananger;
        this.bytes = bytes;
    }
    @Override
    public void run() {
        Handler handler = socketMananger.getHandler();
        if(handler == null){
            return;
        }
        socketMananger.wirteContent(bytes);
    }

    public void writeOutputStream(byte[] bytes,Handler handler){
        boolean socketIsConnect = socketMananger.socketConnect.socketIsConnect();
        Log.e("socketIsConnect 发送",socketIsConnect+"");
        if(socketIsConnect){
            synchronized (socketMananger.writeLock) {
                try {
                    Socket s = null;
                    s = new Socket(Config.HOST, Config.PORT);
                    List<ILocation> iLocationList = ILocation.jsonToList(new String(bytes));
                    if (iLocationList.size() > 0) {
                        for (ILocation iLocation : iLocationList) {
                            String json = actionSocket(ILocation.socketBeanToJson(iLocation), s);
                            Log.e("socket result is上报结果", json);
                        }
                        if (baos != null) {
                            baos.close();
                        }

                    }
                } catch (Exception e) {
                    Log.e("writeOuError",e.getMessage());
                    if (handler != null) {
                        handler.obtainMessage(4).sendToTarget();
                    }
                }
            }
        }

        if (handler != null) {
            handler.obtainMessage(4).sendToTarget();
        }
    }

    public static String  actionSocket(String s1, Socket socket) throws Exception {
        byte[] b1 = s1.getBytes();
        byte[] b = intToByteArray1(b1.length);
        byte[] b2 = new byte[b.length + b1.length];
        System.arraycopy(b, 0, b2, 0, b.length);
        System.arraycopy(b1, 0, b2, b.length, b1.length);
        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
        dos.write(b2);
        dos.flush();
        InputStream in = socket.getInputStream();
        DataInputStream dis = new DataInputStream(in);
        byte[] rheadb = new byte[4];
        dis.read(rheadb);
        int rlen=byteToInt2(rheadb);
        byte[] buffer = new byte[1024];
        baos = new ByteArrayOutputStream();
        for (int i = dis.read(buffer); i > -1; i = dis.read(buffer)) {
            baos.write(buffer, 0, i);
            rlen -= i;
            if (rlen == 0)
                break;
        }
        byte[] result = baos.toByteArray();
        String resultStr = new String(result);
        baos.flush();
        return resultStr;
    }

    public static int byteToInt2(byte[] b) {
        int mask = 0xff;
        int temp = 0;
        int n = 0;
        for (int i = 0; i < 4; i++) {
            n <<= 8;
            temp = b[i] & mask;
            n |= temp;
        }
        return n;
    }

    public static byte[] intToByteArray1(int i) {
        byte[] result = new byte[4];
        result[0] = (byte) ((i >> 24) & 0xFF);
        result[1] = (byte) ((i >> 16) & 0xFF);
        result[2] = (byte) ((i >> 8) & 0xFF);
        result[3] = (byte) (i & 0xFF);
        return result;
    }
}
